extern crate serde_json;
extern crate timedb;

use self::timedb::{client, cmd::doit, VRece, VSend};
use async_channel::unbounded;
use base::Resp;
use lazy_static::lazy_static;
use serde_json::Value;

lazy_static! {
    static ref CLIENT: client::Client = client::Client::new();
    static ref CHANNEL: (VSend, VRece) = unbounded::<Value>();
}

#[allow(dead_code)]
pub async fn dourl(data: &Value, cmd: &str) -> Resp {
    doit(data, cmd, &CLIENT, CHANNEL.0.clone()).await
}

#[allow(dead_code)]
pub fn eq_obj_notime(left: &mut Value, mut right: Value, line: u32, file: &str) {
    let left = left.as_object_mut().unwrap();
    let right = right.as_object_mut().unwrap();

    let left = left.get_mut("data").unwrap();
    let right = right.get_mut("data").unwrap();

    let left = left.as_object_mut().unwrap();
    let right = right.as_object_mut().unwrap();

    println!("{}: {}", file, line);

    for (_, d) in left.iter_mut() {
        let d = d.as_object_mut().unwrap();

        assert_eq!(d.contains_key("start"), true);
        assert_eq!(d.contains_key("end"), true);

        d.remove("start");
        d.remove("end");

        let d = d.get_mut("data").unwrap();
        let d = d.as_array_mut().unwrap();

        for v in d.iter_mut() {
            let v = v.as_object_mut().unwrap();
            v.remove("time");
        }
    }

    assert_eq!(left, right);
}
